package com.servlet;

import com.beans.AdminInfo;
import com.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private AdminDao _admindao=new AdminDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminName=request.getParameter("adminName");
        String password=request.getParameter("password");
        AdminInfo admin=_admindao.login(adminName,password);
        if(admin!=null){
            request.getSession().setAttribute("admin",admin);
            request.getRequestDispatcher("/main.jsp").forward(request,response);
        }else {
            request.setAttribute("msg","用户登录失败");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}
